package models

////////////////////////////项目////////////////////////////////

//查询
func QueryPrjAll() [] ItemPrj {
	data_list := [] ItemPrj{}
	db.Order("prj_name").Find(&data_list)
	return data_list
}

func GetRolePrj(ids []int) []ItemPrj {
	//获取平台
	role_list := [] Role{}
	db.Where("id in (?)",ids).Find(&role_list)
	var prj_id_list []int
	for _,v := range role_list{
		if v.RoleName == "admin"{
			return  QueryPrjAll()
			break
		}else{
			var Platfm_list []ItemPlatfm
			db.Model(&v).Related(&Platfm_list, "PlatForm")
			for _,vv:= range Platfm_list{
				prj_id_list = append(prj_id_list,vv.PrjId)
			}
		}
	}
	var data_list []ItemPrj
	db.Where("id in (?)",prj_id_list).Find(&data_list)
	return  data_list
}

//添加
func AddPrj(obj ItemPrj) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryPrjOne(id int) ItemPrj {
	obj := ItemPrj{}
	db.First(&obj,id)
	return obj
}

//更新角色
func UpdatePrj(id int,prj ItemPrj) error {
	obj := ItemPrj{}
	err := db.First(&obj,id).Update(&prj).Error
	return err
}

//删除
func DeletePrj(id int) error {
	obj := ItemPrj{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}

// 搜索
func QueryPrjSearch(key string) []ItemPrj {
	obj := [] ItemPrj{}
	db.Where("prj_name LIKE ?", "%"+key+"%").Find(&obj)
	return obj
}

// 搜索
func GetPrjName(key string) ItemPrj {
	obj := ItemPrj{}
	db.Where("prj_name = ?", key).First(&obj)
	return obj
}

////////////////////////////平台////////////////////////////////

//查询
func QueryPlatfmAll(role_ids []int) [] ItemPlatfm {
	role_list := [] Role{}
	db.Where("id in (?)",role_ids).Find(&role_list)
	var data_list []ItemPlatfm
	var pfm_ids []int
	for _,v := range role_list{
		if v.RoleName == "admin"{
			db.Order("prj_id").Preload("Prj").Find(&data_list)
			return data_list
			break
		}else{
			var Platfm_list []ItemPlatfm
			db.Model(&v).Related(&Platfm_list, "PlatForm")
			for _,j :=range Platfm_list{
				pfm_ids = append(pfm_ids, j.ID)
			}
		}

	}
	db.Order("prj_id").Preload("Prj").Where("id in (?)",pfm_ids).Find(&data_list)
	return data_list
}

//添加
func AddPlatfm(obj ItemPlatfm) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryPlatfmOne(id int) ItemPlatfm {
	obj := ItemPlatfm{}
	db.First(&obj,id)
	return obj
}

//更新
func UpdatePlatfm(id int,platfm ItemPlatfm) error {
	obj := ItemPlatfm{}
	err := db.First(&obj,id).Update(&platfm).Error
	return err
}

//删除
func DeletePlatfm(id int) error {
	obj := ItemPlatfm{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}


// 搜索
func QueryPlatfmSearch(key string) []ItemPlatfm {
	obj := [] ItemPlatfm{}
	db.Preload("Prj").Where("platfm_name LIKE ?", "%"+key+"%").Find(&obj)
	return obj
}


// 搜索
func GetPlatfm(prj_id int,barch string) (ItemPlatfm,error) {
	obj := ItemPlatfm{}
	err:=db.Where("prj_id = ?", prj_id).Where("code_version = ?",barch).First(&obj).Error
	return obj,err
}


// 搜索
func GetPlatfmList(prj_id int) []ItemPlatfm {
	obj := []ItemPlatfm{}
	db.Where("prj_id = ?", prj_id).Find(&obj)
	return obj
}


////////////////////////////版本发布////////////////////////////////
//查询
func QueryReleaseAll(role_id_list []int) [] ItemRelease {
	data_list := [] ItemRelease{}
	Platfm_obj := QueryPlatfmAll(role_id_list)
	for _,v :=range Platfm_obj{
		var release ItemRelease
		err:=db.Where("platfm_id = ?",v.ID).Preload("Platfm").Preload("Platfm.Prj").First(&release).Error
		if err == nil{
			data_list = append(data_list, release)
		}
	}
	return data_list
}

//查询
func QueryReleaseSearch(key string) [] ItemRelease {
	data_list := [] ItemRelease{}
	Platfm_obj := QueryPlatfmSearch(key)
	for _,v :=range Platfm_obj{
		var release ItemRelease
		err:=db.Where("platfm_id = ?",v.ID).Preload("Platfm").Preload("Platfm.Prj").First(&release).Error
		if err == nil{
			data_list = append(data_list, release)
		}
	}
	return data_list
}

//添加
func AddRelease(obj ItemRelease) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryReleaseOne(id int) ItemRelease {
	obj := ItemRelease{}
	db.Preload("Platfm").Preload("Platfm.Prj").First(&obj,id)
	return obj
}

//更新
func UpdateRelease(id int,release ItemRelease) error {
	obj := ItemRelease{}
	err := db.First(&obj,id).Update(&release).Error
	return err
}

//删除
func DeleteRelease(id int) error {
	obj := ItemRelease{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}


//通过ID查询
func GetReleaseId(id int) ItemRelease {
	obj := ItemRelease{}
	db.Preload("Platfm").Preload("Platfm.Prj").Where("platfm_id = ?",id).First(&obj)
	return obj
}

////////////////////////////分支管理////////////////////////////////


//查询
func QueryBranchCount(is_super bool,cur_user string,role_ids []int) int{
	data_list := [] ItemBranch{}
	var count int
	if is_super{
		db.Order("created_at desc").Limit(12).Find(&data_list).Count(&count)
	}else{

		pfm_objs :=QueryPlatfmAll(role_ids)
		var pfm_ids []int
		for _,v := range pfm_objs{
			pfm_ids = append(pfm_ids, v.ID)
		}

		db.Order("created_at desc").Where("platfm_id in (?)",pfm_ids).Limit(12).Find(&data_list).Count(&count)
	}
	return count
}



//查询
func QueryBranchAll(page int,is_super bool,cur_user string,role_id_list []int) [] ItemBranch {
	data_list := [] ItemBranch{}
	var Platfm_obj [] ItemPlatfm
	if is_super{
		Platfm_obj = QueryPlatfmAll(role_id_list)
	}else{
		//未完善
		Platfm_obj = QueryPlatfmAll(role_id_list)
	}
	for _,v :=range Platfm_obj{
		var branch_list []ItemBranch
		err:=db.Where("platfm_id = ?",v.ID).Preload("Platfm").Preload("Platfm.Prj").Find(&branch_list).Error
		if err == nil{
			for _,v := range branch_list{
				data_list = append(data_list, v)
			}

		}
	}

	if page ==1{
		if 12 <=len(data_list){
			return data_list[0:12]
		}else{
			return data_list[0:]
		}
	}else{
		N := 12*(page-1)
		if 12*(page) <=len(data_list){
			return data_list[N:12*(page)]
		}else{

			return data_list[N:]
		}
	}
}


func QueryBranchSearch(key string) [] ItemBranch {
	data_list := [] ItemBranch{}
	Platfm_obj := QueryPlatfmSearch(key)
	for _,v :=range Platfm_obj{
		var branch_list []ItemBranch
		err:=db.Where("platfm_id = ?",v.ID).Preload("Platfm").Preload("Platfm.Prj").Find(&branch_list).Error
		if err == nil{
			for _,v := range branch_list{
				data_list = append(data_list, v)
			}
		}
	}
	return data_list
}

//添加
func AddBranch(obj ItemBranch) error {
	err := db.Create(&obj).Error
	return err
}

//通过ID查询
func QueryBranchOne(id int) ItemBranch {
	obj := ItemBranch{}
	db.Preload("Platfm").Preload("Platfm.Prj").First(&obj,id)
	return obj
}

//更新
func UpdateBranch(id int,release ItemBranch) error {
	obj := ItemBranch{}
	err := db.First(&obj,id).Update(&release).Error
	return err
}

//更新
func UpdateBranchOfName(prj_name,branch_name string,branch ItemBranch)  {
	prj_obj := GetPrjName(prj_name)
	pfm_list := GetPlatfmList(prj_obj.ID)
	for _,v:=range pfm_list{
		branch_obj,err :=GetBranchOfName(v.ID,branch_name)
		if err == nil{
			UpdateBranch(branch_obj.ID,branch)
		}
	}
}


//删除
func DeleteBranch(id int) error {
	obj := ItemBranch{}
	err := db.First(&obj,id).Delete(&obj).Error
	return err
}

//删除
func GetBranchOfName(pfm_id int,branch_name string) (ItemBranch,error) {
	obj := ItemBranch{}
	err := db.Preload("Platfm").Preload("Platfm.Prj").Where("platfm_id = ?",pfm_id).Where("branch_name = ?",branch_name).First(&obj).Error
	return obj,err
}


//通过ID查询
func GetBranchId(id int) ItemBranch {
	obj := ItemBranch{}
	db.Preload("Platfm").Preload("Platfm.Prj").Where("platfm_id = ?",id).First(&obj)
	return obj
}
